/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.wizards.core.events.investiture;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;
import org.openide.WizardDescriptor;
import org.openide.util.HelpCtx;
import pt.iasd.youthshelter.datamodel.Investiture;
import pt.iasd.youthshelter.wizards.core.WizardKeys;

public class EditInvestitureWizardPanel implements WizardDescriptor.Panel<WizardDescriptor>, PropertyChangeListener {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/wizards/core/Bundle");

    /**
     * The visual component that displays this panel. If you need to access the
     * component from this class, just use getComponent().
     */
    private InvestitureVisualPanel component;
    
    private WizardDescriptor descriptor;
    
    private final EventListenerList listeners = new EventListenerList();
    
    private boolean isValid = false;
    
    private List<Investiture> distinctInvestitures;
    
    private String oldPlace;
    private Date oldDate;

    // Get the visual component for the panel. In this template, the component
    // is kept separate. This can be more efficient: if the wizard is created
    // but never displayed, or not all panels are displayed, it is better to
    // create only those which really need to be visible.
    @Override
    public InvestitureVisualPanel getComponent() {
        if (component == null) {
            component = new InvestitureVisualPanel(oldPlace, oldDate);
        }
        component.putClientProperty(WizardDescriptor.PROP_AUTO_WIZARD_STYLE, true);
        return component;
    }

    @Override
    public HelpCtx getHelp() {
        // Show no Help button for this panel:
        return HelpCtx.DEFAULT_HELP;
        // If you have context help:
        // return new HelpCtx("help.key.here");
    }

    @Override
    public boolean isValid() {
        // If it is always OK to press Next or Finish, then:
        return isValid;
        // If it depends on some condition (form filled out...) and
        // this condition changes (last form field filled in...) then
        // use ChangeSupport to implement add/removeChangeListener below.
        // WizardDescriptor.ERROR/WARNING/INFORMATION_MESSAGE will also be useful.
    }

    @Override
    public void addChangeListener(ChangeListener l) {
        listeners.add(ChangeListener.class, l);
    }

    @Override
    public void removeChangeListener(ChangeListener l) {
        listeners.remove(ChangeListener.class, l);
    }

    @Override
    public void readSettings(WizardDescriptor wiz) {
        this.descriptor = wiz;
        this.oldPlace = (String)wiz.getProperty(WizardKeys.PROP_PLACE);
        this.oldDate = (Date)wiz.getProperty(WizardKeys.PROP_DATE);
        this.distinctInvestitures = (List<Investiture>)wiz.getProperty(WizardKeys.PROP_LIST);
        getComponent().addPropertyChangeListener(this);
    }

    @Override
    public void storeSettings(WizardDescriptor wiz) {
        wiz.putProperty(WizardKeys.PROP_PLACE, component.getPlaceTextField().getText());
        wiz.putProperty(WizardKeys.PROP_DATE, component.getEventDatePicker().getDate());
        wiz.putProperty(WizardKeys.PROP_INVESTITURE_LIST, component.getInvestitures());
        wiz.putProperty(WizardKeys.PROP_EDIT_LIST, component.getEditedInvestitures());
        wiz.putProperty(WizardKeys.PROP_REMOVE_LIST, component.getRemovedInvestitures());
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        boolean oldState = isValid;
        String place = component.getPlaceTextField().getText();
        Date date = component.getEventDatePicker().getDate();
        if(place == null || place.isEmpty()) {
            isValid = false;
            descriptor.getNotificationLineSupport().setErrorMessage(
                    bundle.getString("Wizard.Place.IsEmpty"));
        } else if(date == null) {
            isValid = false;
            descriptor.getNotificationLineSupport().setErrorMessage(
                    bundle.getString("Wizard.Date.IsEmpty"));/*
        } else if(dateOverlaps(date, place)) {
            isValid = false;
            SimpleDateFormat formatter = new SimpleDateFormat("dd-mm-yyyy");
            descriptor.getNotificationLineSupport().setErrorMessage(
                String.format(bundle.getString("Investiture.Exists.Error"),
                    place, formatter.format(date)));*/
        } else {
            List<Investiture> investitures = component.getInvestitures();
            if(investitures == null || investitures.isEmpty()) {
                isValid = false;
                descriptor.getNotificationLineSupport().setErrorMessage(
                        bundle.getString("Investiture.NoPathfinder.Error"));
            } else if(place.equalsIgnoreCase(oldPlace) && date.equals(oldDate)) {
                isValid = false;
                descriptor.getNotificationLineSupport().setWarningMessage(
                        bundle.getString("Investiture.NoChange.Error"));
            } else {
                isValid = true;
                descriptor.getNotificationLineSupport().clearMessages();
            }
        }
        if(oldState != isValid) {
            ChangeEvent event = new ChangeEvent(this);
            for(ChangeListener listener : listeners.getListeners(ChangeListener.class)) {
                listener.stateChanged(event);
            }
        }
    }
    
    // <editor-fold defaultstate="collapsed" desc="Date Analisys Code">
    private boolean dateOverlaps(Date eventDate, String place) {
        if(distinctInvestitures != null) {
            for(Investiture investiture : distinctInvestitures) {
                if(investiture.getPlace().equalsIgnoreCase(place)
                        && investiture.getEventDate().equals(eventDate)) {
                    return true;
                }
            }
        }
        return false;
    }// </editor-fold>
}
